package com.snake.vargoappstore.widget;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

/**配合TabItem使用，带消息提示的指示器*/
public class TabView extends LinearLayout implements View.OnClickListener{

    /**选中字体颜色*/
    private static final int sTextColorSelect = 0xff1683f8;
    /**默认字体颜色*/
    private static final int sTextColorNormal = 0xff9e9d9d;
    /**默认字体大小*/
    private static final int sTextSize = 9;
    private ViewPager mViewPager;
    private String[] mTitle ;
    private int[] mIconSelect ;
    private int[] mIconNormal ;

    public TabView(Context context) {
        super(context);
    }

    public TabView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TabView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    /**必须调用的方法在setResource(...)之后*/
    public void setViewPager(ViewPager viewPager, final CustomBar customBar){
        mViewPager = viewPager;
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //positionOffset不为零，就一定有下一页。
                if(positionOffset!=0){
                    View leftView = mViewPager.getChildAt(position);
                    View rightView = mViewPager.getChildAt(position + 1);
                    leftView.setAlpha(1-positionOffset);
                    rightView.setAlpha(positionOffset);

                    ((TabItem)getChildAt(position)).setTabAlpha(1-positionOffset);
                    ((TabItem)getChildAt(position+1)).setTabAlpha(positionOffset);
                }else{
                    mViewPager.getChildAt(position).setAlpha(1);
                    ((TabItem)getChildAt(position)).setTabAlpha(1);
                }
            }

            @Override
            public void onPageSelected(int position) {
                customBar.updateCustomBar(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        initView();
    }

    private void initView() {
        int childCount = mViewPager.getAdapter().getCount();
        for (int i=0;i<childCount;i++){
            if(mIconSelect==null||mIconNormal==null||mTitle==null){
                throw new RuntimeException("setResource()  must be called before setViewPager()");
            }
            TabItem tabItem = new TabItem(getContext());
            LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT,1);
            tabItem.setIconAndText(new int[]{mIconSelect[i],mIconNormal[i]},mTitle[i]);
            tabItem.setTextSize(sTextSize);
            tabItem.setTextColorNormal(sTextColorNormal);
            tabItem.setTextColorSelect(sTextColorSelect);
            tabItem.setTag(i);
            tabItem.setOnClickListener(this);
            addView(tabItem,layoutParams);
            initTabView(i, tabItem);

        }
    }

    private void initTabView(int i, TabItem tabItem) {
        if(i==0){
            tabItem.setTabAlpha(1);
        }else {
            tabItem.setTabAlpha(0);
        }
    }

    @Override
    public void onClick(View v) {
        TabItem tabItem = (TabItem) v;
        int position = (int) tabItem.getTag();
        updateViewPager(position);
        updateTabView(tabItem, position);

    }

    private void updateTabView(TabItem tabItem, int position) {
        for(int i=0;i<getChildCount();i++){
            if(position==i){
                tabItem.setTabAlpha(1);
            }else{
                View child = getChildAt(i);
                ((TabItem)child).setTabAlpha(0);
            }
        }
    }

    private void updateViewPager(int position) {
        if(mViewPager.getCurrentItem()!=position){
            mViewPager.setCurrentItem(position,false);
        }
    }

    /**
     * 必须调用的方法，把需要的资源，图片和文本传递过来，并且在setViewPager(...)之前调用
     * */
    public void setResource(int [] iconSelect,int[] iconNormal,String[] title ){
        this.mIconSelect = iconSelect;
        this.mIconNormal = iconNormal;
        this.mTitle = title;
    }
}
